package com.xinruke.hostels.systemmanage.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.cache.vo.LoginManageResultVO;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.XtBanci;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.system.manage.interfaces.dto.*;
import com.xinruke.hostels.systemmanage.mapper.XtBanciMapper;
import com.xinruke.hostels.systemmanage.service.WorkTurnService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yutaipu
 */
@Service
public class WorkTurnServiceImpl implements WorkTurnService {

    @Autowired
    private XtBanciMapper xtBanciMapper;

    @Autowired
    private ManageUserCache manageUserCache;

    @Override
    public ResultVO add(WorkTurnAddDTO params) {
        ResultVO resultVO = ResultVO.success();

        checkIsCanAdd(params, resultVO);
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }

        LoginManageResultVO user = manageUserCache.getLoginManageUserInfo();

        XtBanci add = new XtBanci();
        BeanUtils.copyProperties(params, add);
        add.setJiudianId(user.getJiudianId());
        add.setChuangjianshijian(new Date());
        add.setChuangjianrenId(user.getId());
        add.setGengxinrenId(user.getId());
        add.setGengxinshijian(new Date());
        add.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
        int insert = xtBanciMapper.insertSelective(add);
        if (insert == 0) {
            return ResultVO.fail();
        }
        return resultVO;
    }

    @Override
    public ResultVO modify(WorkTurnModifyDTO params) {
        ResultVO resultVO = ResultVO.success();

        checkIsCanModify(params, resultVO);
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }
        XtBanci modify = new XtBanci();
        BeanUtils.copyProperties(params, modify);
        modify.setGengxinrenId(manageUserCache.getCurrentUserId());
        modify.setGengxinshijian(new Date());
        int count = xtBanciMapper.updateByPrimaryKeySelective(modify);
        if (count == 0) {
            return ResultVO.fail();
        }
        return resultVO;
    }

    @Override
    public ResultVO delete(WorkTurnDeleteDTO params) {
        ResultVO resultVO = ResultVO.success();

        checkIsCanDelete(params, resultVO);
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }
        XtBanci delete = new XtBanci();
        BeanUtils.copyProperties(params, delete);
        delete.setGengxinshijian(new Date());
        delete.setGengxinrenId(manageUserCache.getCurrentUserId());
        delete.setShanchubiaoshi(SysDicConstant.DELFLAG_1);
        int insert = xtBanciMapper.updateByPrimaryKeySelective(delete);
        if (insert == 0) {
            return ResultVO.fail();
        }
        return resultVO;
    }

    @Override
    public ResultVO<RowsDataVO<WorkTurnQueryResultDTO>> list(WorkTurnQueryListDTO params) {

        RowsDataVO<WorkTurnQueryResultDTO> dataVO = new RowsDataVO();
        QueryConditions conditions = new QueryConditions();

        conditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        conditions.addCondition("jiudianId", EConditionOperator.EQULS, manageUserCache.getJiudianId());

        MyExample example = new MyExample(XtBanci.class, conditions, params.getOrderFields());
        Page<XtBanci> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<XtBanci> list = xtBanciMapper.selectByExample(example);

        List<WorkTurnQueryResultDTO> dtoList = list.stream().map(n -> {
            WorkTurnQueryResultDTO dto = new WorkTurnQueryResultDTO();
            BeanUtils.copyProperties(n, dto);
            return dto;
        }).collect(Collectors.toList());

        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(dtoList);
        return ResultVO.success("查询成功", dataVO);
    }

    @Override
    public ResultVO<WorkTurnQueryResultDTO> detail(WorkTurnDetailDTO params) {

        WorkTurnQueryResultDTO dto = new WorkTurnQueryResultDTO();
        XtBanci detail = xtBanciMapper.selectByPrimaryKey(params.getId());

        if (null == detail) {
            return ResultVO.fail("没有此记录");
        }
        BeanUtils.copyProperties(detail, dto);
        return ResultVO.success("获取详情成功", dto);
    }

    private void checkIsCanModify(WorkTurnModifyDTO params, ResultVO resultVO) {
    }

    private void checkIsCanAdd(WorkTurnAddDTO params, ResultVO resultVO) {
    }

    private void checkIsCanDelete(WorkTurnDeleteDTO params, ResultVO resultVO) {
    }
}
